#include "OADataCebter.h"

#include <iostream>
#include <string>
#include <vector>

using namespace std;

// 单例模式的实现
OADataCebter OADataCebter::_instance = OADataCebter();
// 静态成员函数的实现
OADataCebter &OADataCebter::getInstance() { return _instance; }

OADataCebter::OADataCebter() {
  _admins.push_back(OAAdmin("admin", "123456"));
  _users.push_back(OAUser("zhangsan", "123456", "人力资源部"));
  _users.push_back(OAUser("lisi", "123456", "开发部"));
}

/**
 * @brief 管理员登录
 *
 * @param username
 * @param password
 * @return OAAdmin&
 */
OAAdmin &OADataCebter::loginAdmin(string username, string password) {
  for (OAAdmin &admin : _admins) {
    if (admin._username == username && admin._password == password) {
      return admin;
    }
  }
  return OAAdmin::ERROR_ADMIN;
}

/**
 * @brief 用户登录
 *
 * @param username
 * @param password
 * @return OAUser&
 */
OAUser &OADataCebter::loginUser(string username, string password) {
  for (OAUser &user : _users) {
    if (user._username == username && user._password == password) {
      return user;
    }
  }
  return OAUser::ERROR_USER;
}

/**
 * @brief 添加普通用户
 *
 * @param username
 * @param password
 * @param department
 * @return true
 * @return false
 */
bool OADataCebter::addUser(string username, string password,
                           string department) {
  // 检查用户是否已经存在
  for (OAUser &user : _users) {
    if (user._username == username) {
      return false;
    }
  }
  _users.push_back(OAUser(username, password, department));
  return true;
}

/**
 * @brief 删除普通用户
 *
 * @param username
 * @return true
 * @return false
 */
bool OADataCebter::delUser(string username) {
  for (auto it = _users.begin(); it != _users.end(); it++) {
    if (it->_username == username) {
      _users.erase(it);
      return true;
    }
  }
  return false;
}

/**
 * @brief 添加会议室
 *
 * @param mid
 * @param capacity
 * @param roomname
 * @return true
 * @return false
 */
bool OADataCebter::addMeetingRoom(int mid, int capacity, string roomname) {
  // 检查会议室是否已经存在
  for (OAMeetingRoom &room : _meetingRooms) {
    if (room.mid() == mid) {
      return false;
    }
  }
  _meetingRooms.push_back(OAMeetingRoom(mid, capacity, roomname));
  return true;
}

/**
 * @brief 删除会议室
 *
 * @param mid
 * @return true
 * @return false
 */
bool OADataCebter::delMeetingRoom(int mid) {
  for (auto it = _meetingRooms.begin(); it != _meetingRooms.end(); it++) {
    if (it->mid() == mid) {
      _meetingRooms.erase(it);
      return true;
    }
  }
  return false;
}

/**
 * @brief 清空会议室状态
 *
 */
void OADataCebter::clearMeetingRoomType() {
  for (OAMeetingRoom &room : _meetingRooms) {
    room.roomtype(-1);
  }
}

/**
 * @brief 预约会议室
 *
 * @param mid
 * @param username
 * @return true
 * @return false
 */
bool OADataCebter::reserveMeetingRoom(int mid, string username) {
  for (OAMeetingRoom &room : _meetingRooms) {
    if (room.mid() == mid) {
      return room.roomtype(0, username);
    }
  }
  return false;
}

/**
 * @brief 取消预约会议室
 *
 * @param mid
 * @param username
 * @return true
 * @return false
 */
bool OADataCebter::cancelReserveMeetingRoom(int mid, string username) {
  for (OAMeetingRoom &room : _meetingRooms) {
    if (room.mid() == mid && room.username() == username) {
      return room.roomtype(-1);
    }
  }
  return false;
}

/**
 * @brief 查看用户预约的会议室
 *
 * @param username
 */
void OADataCebter::printMeetingRoomByUser(string username) {
  for (OAMeetingRoom &room : _meetingRooms) {
    if (room.username() == username) {
      cout << room.mid() << "\t" << room.capacity() << "\t" << room.roomname()
           << "\t" << room.roomtype_name() << endl;
    }
  }
}

/**
 * @brief 输出所有的普通用户
 *
 */
void OADataCebter::printUser() {
  for (OAUser &user : _users) {
    cout << user._username << "\t" << user._department << endl;
  }
}

/**
 * @brief 输出所有的会议室
 *
 */
void OADataCebter::printMeetingRoom() {
  for (OAMeetingRoom &room : _meetingRooms) {
    cout << room.mid() << "\t" << room.capacity() << "\t" << room.roomname()
         << "\t" << room.roomtype_name() << endl;
  }
}
